﻿Imports NUnit.Framework
Imports Rhino.Mocks
Imports System.Web.Routing

<TestFixture()> _
Public Class XhtmlHelperTest

    Public Const AppPathModifier = "/$(SESSION)"

    <Test()> _
    Public Sub ViewContextProperty()
        Dim viewContext = GetViewContext(MockRepository.GenerateMock(Of RouteData))
        Dim xhtmlHelper = New XhtmlHelper(viewContext, MockRepository.GenerateMock(Of IViewDataContainer)())

        Dim value = xhtmlHelper.ViewContext

        Assert.AreSame(viewContext, value)
    End Sub

    <Test()> _
    <ExpectedException(GetType(ArgumentNullException))> _
    Public Sub ConstructorWithNullViewContextThrows()
        Dim xhtmlHelper = New XhtmlHelper(Nothing, MockRepository.GenerateMock(Of IViewDataContainer)())
    End Sub

    <Test()> _
    <ExpectedException(GetType(ArgumentNullException))> _
    Public Sub ConstructorWithNullViewDataContainerThrows()
        Dim viewContext = GetViewContext(MockRepository.GenerateMock(Of RouteData))
        Dim xhtmlHelper = New XhtmlHelper(viewContext, Nothing)
    End Sub

    <Test()> _
    Public Sub MockHttpContextResponseApplyAppPathModifier()
        Dim httpContext = GetHttpContext("/app/", "", "")
        Const virtualPath = "/foo"
        Dim r = httpContext.Response.ApplyAppPathModifier(virtualPath)
        Assert.AreEqual(AppPathModifier + virtualPath, r)
    End Sub

    Private Shared Function GetViewContext(ByVal routeData As RouteData) As System.Web.Mvc.ViewContext
        Dim viewContext = MockRepository.GenerateMock(Of ViewContext)()
        If routeData IsNot Nothing Then
            viewContext.Expect(Function(c) c.RouteData).Return(routeData)
            Dim vdd = New ViewDataDictionary()
            viewContext.Expect(Function(c) c.ViewData).Return(vdd)
            Dim httpContext = GetHttpContext("/app/", "", "")
            viewContext.Expect(Function(c) c.HttpContext).Return(httpContext)
        End If
        Return viewContext
    End Function

    Private Shared Function GetHttpContext(ByVal appPath As String, _
                                               ByVal requestPath As String, _
                                               ByVal httpMethod As String, _
                                               Optional ByVal protocol As String = "http", _
                                               Optional ByVal port As Integer = -1 _
                                               ) As HttpContextBase
        Dim mockHttpContext = MockRepository.GenerateMock(Of HttpContextBase)()

        Dim request = MockRepository.GenerateMock(Of HttpRequestBase)()

        If Not String.IsNullOrEmpty(appPath) Then
            request.Expect(Function(r) r.ApplicationPath).Return(appPath)
        End If

        If Not String.IsNullOrEmpty(requestPath) Then
            request.Expect(Function(r) r.AppRelativeCurrentExecutionFilePath).Return(requestPath)
        End If

        Dim uri As Uri

        If port >= 0 Then
            uri = New Uri(protocol + "://localhost" + ":" + port)
        Else
            uri = New Uri(protocol + "://localhost")
        End If

        request.Expect(Function(r) r.Url).Return(uri)

        request.Expect(Function(r) r.PathInfo).Return("")

        If Not String.IsNullOrEmpty(httpMethod) Then
            request.Expect(Function(r) r.HttpMethod).Return(httpMethod)
        End If

        mockHttpContext.Expect(Function(hc) hc.Request).Return(request)

        Dim response = MockRepository.GenerateMock(Of HttpResponseBase)()

        response.Expect(Function(r) r.ApplyAppPathModifier(Arg(Of String).Is.Anything)).Do(Function(s As String) AppPathModifier + s)

        mockHttpContext.Expect(Function(hc) hc.Response).Return(response)

        Return mockHttpContext
    End Function

End Class
